package com.archetypus.projectschedule.web.involvements.cmd;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.archetypus.projectschedule.common.Configuration;
import com.archetypus.projectschedule.common.dto.activities.Activity;
import com.archetypus.projectschedule.common.dto.activities.ActivityKey;
import com.archetypus.projectschedule.common.dto.involvements.InvolvementListHandler;
import com.archetypus.projectschedule.common.dto.projects.Project;
import com.archetypus.projectschedule.common.dto.projects.ProjectKey;
import com.archetypus.projectschedule.common.exceptions.ProjectSchedulerException;
import com.archetypus.projectschedule.web.activities.bd.ActivityBD;
import com.archetypus.projectschedule.web.activities.bd.IActivityBD;
import com.archetypus.projectschedule.web.common.cmd.ICommand;
import com.archetypus.projectschedule.web.involvements.bd.IInvolvementBD;
import com.archetypus.projectschedule.web.involvements.bd.InvolvementBD;
import com.archetypus.projectschedule.web.projects.bd.IProjectBD;
import com.archetypus.projectschedule.web.projects.bd.ProjectBD;

public class ListInvolvementCmd implements ICommand {

	public void execute(HttpServletRequest request, HttpServletResponse response)throws ProjectSchedulerException {
		int activityId = Integer.parseInt(request.getParameter("activity"));
		int projectId = Integer.parseInt(request.getParameter("project"));
		
		ProjectKey projectKey = new ProjectKey(projectId);
		ActivityKey activityKey = new ActivityKey(projectKey,activityId);
		
		Project project = new Project();
		project.setProjectKey(projectKey);
		
		Activity activity = new Activity();
		activity.setActivityKey(activityKey);
		
		IInvolvementBD involvementBD = new InvolvementBD();
		InvolvementListHandler listHandler = involvementBD.listInvolvementsByActivity(activity);
		
		IActivityBD activityBD = new ActivityBD();
		Activity selectedActivity = activityBD.findActivity(activity);
		selectedActivity.setContribution(listHandler.getList());
		
		IProjectBD projectBD = new ProjectBD();
		Project selectedProject = projectBD.findProject(project);
		
		request.setAttribute("project", selectedProject);
		request.setAttribute("activity", selectedActivity);
		request.setAttribute("activityDetail", listHandler.getList());
		request.setAttribute("pageSize", Configuration.getInstance().get("pageSize"));
	}

}
